//
//  AsViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/7/10.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class AsViewController: NSViewController {
    
    var sTitle:String = ""
    var msg:String = ""
    var btnTitles:[String] = []
    var touchAction:ResultInt?
    
    var subBtns:[ZHButton] = []
    var kWidth:CGFloat = 300
    var kBtnWidth:CGFloat = 80
    var kBtnHeight:CGFloat = 26
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, 440, 180))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.view.window?.styleMask = [.fullSizeContentView]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    convenience init(title:String,msg:String,btnTitles:[String],touchAction:ResultInt?) {
        self.init()
        self.sTitle = title
        self.msg = msg
        self.btnTitles = btnTitles
        self.touchAction = touchAction
        self.setupUI()
        self.loadData()
    }
    func loadData() {
        self.sTitleLab.stringValue = self.sTitle
        self.contentLab.stringValue = self.msg
    }
    
    func setupUI() {
        
        self.view.addSubview(sTitleLab)
        sTitleLab.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
        }
        
        self.view.addSubview(contentLab)
        contentLab.snp.makeConstraints { (make) in
            make.height.equalTo(50)
            make.centerX.equalToSuperview()
            make.top.equalTo(sTitleLab.snp.bottom).offset(15)
            make.width.equalTo(300)
        }
        
        self.view.addSubview(containr)
        containr.snp.makeConstraints { (make) in
            make.width.equalTo(kWidth)
            make.bottom.equalTo(-20)
            make.height.equalTo(36)
            make.centerX.equalToSuperview()
        }
        
        self.subBtns = self.initView(inView: containr, titles:self.btnTitles)
    }
    
    lazy var sTitleLab: NSLabel = {
        let lab = NSLabel.label(title: "结束会议", textColor: NSColor.black, font: NSFont.systemFont(ofSize: 16))
        lab.sizeToFit()
        lab.alignment = .center
        return lab
    }()
    
    
    lazy var contentLab: NSLabel = {
        let lab = NSLabel.label(title: "如果您不想结束会议，请在点击“离开会议“前指定新的主持人。", textColor: color_gray(180), font: NSFont.systemFont(ofSize: 13))
        lab.alignment = .center
        lab.maximumNumberOfLines = 2
        return lab
    }()
    
    lazy var containr: NSView = {
        let view = NSView()
        return view
    }()
    
    func initView(inView:NSView,titles:[String]) -> [ZHButton] {
        let space = (self.kWidth-CGFloat(titles.count*90))/CGFloat(titles.count-1)
        var subBtns:[ZHButton] = []
        for (inx,val) in titles.enumerated() {
            let btn = ZHButton.init()
            btn.zh_title = val
            btn.zh_font = NSFont.systemFont(ofSize: 13)
            btn.hasBorder = true
            if inx == titles.indexLast {
                btn.zh_color = color_gray(255)
                btn.zh_backgroundColor = color_main108
                btn.hasBorder = true
                btn.zh_cornerRadius = 0
                btn.keyEquivalent = "\r"
            }else {
                btn.normalColor = color_gray(40)
                btn.selectedColor = color_gray(40)
                btn.hoverColor = color_main108
                btn.highlightColor = color_main108
                btn.backgroundNormalColor = color_gray(250)
                btn.backgroundSelectedColor = color_gray(250)
                btn.backgroundHoverColor = color_rgba(r: 220, g: 230, b: 250)
                btn.backgroundHighlightColor = color_rgba(r: 220, g: 230, b: 250)
                btn.hasBorder = true
                btn.zh_cornerRadius = 0
                btn.borderNormalWidth = 1
                btn.borderSelectedWidth = 1
                btn.borderHighlightWidth = 0
                btn.borderHoverWidth  = 0
                btn.zh_borderColor = color_gray(230)
            }
            btn.tag = inx
            btn.target = self
            btn.action = #selector(selectBtnAction(senber:))
            inView.addSubview(btn)
            subBtns.append(btn)
            
            btn.snp.makeConstraints { (make) in
                make.height.equalTo(24)
                make.width.equalTo(90)
                make.centerY.equalToSuperview()
                if inx == 0 {
                    make.left.equalTo(0)
                }else {
                    make.left.equalTo(subBtns[inx-1].snp.right).offset(space)
                }
            }
        }
        return subBtns
    }
    
    
    @objc func selectBtnAction(senber:ZHButton) {
        if touchAction != nil {
            self.dismiss(nil)
            self.touchAction!(senber.tag)
        }
    }
}
